package com.redpixy.monit.common.util;



import java.util.HashMap;
import java.util.Map;

import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 
 * 
 * 
 * @author ZhangLei.
 * @version 2008-11-11
 * 
 * @category 读取游戏参数
 * 
 */
public class MonitConfigUtil {

	public static final Logger log = LoggerFactory.getLogger(MonitConfigUtil.class);
	/**
	 * XML文件名
	 */
	private static final String CONFIG_FILE = "monit_config.xml";
	/**
	 * 根配置
	 */
	public static Element _rootElement = XMLUtil.getXMLDocument(CONFIG_FILE).getRootElement(); 

	/**
	 * 得到XML配置DOM 
	 * 
	 * @return
	 */
	public static Element getRootElement() {
		return _rootElement;
	}

	/**
	 * 得到节点路径下的　Element
	 * 
	 * @param str
	 * @return
	 */
	public static Element getElement(String str) {
		String[] paths = str.split("\\.");
		Element el = _rootElement;
		for (int i = 0; i < paths.length; i++) {
			String path = paths[i];
			el = el.element(path);
		}
		return el;
	}

	/**
	 * 获取xml中的String值.
	 * 
	 * @param str
	 * @return
	 */
	public static String getString(String str) {
		try {
			String[] paths = str.split("\\.");
			Element el = _rootElement;
			for (int i = 0; i < paths.length - 1; i++) {
				String path = paths[i];
				el = el.element(path);
			}
			String val = "";
			Element coe = el.element(paths[paths.length - 1]);
			if (coe == null) {
				val = el.attributeValue(paths[paths.length - 1]);
			} else {
				val = coe.getTextTrim();
			}
			if ("".equals(val)) {
				log.error("----->没有定义参数值 -(" + str + ")", coe.asXML());
			}
			return val;
		} catch (Exception e) {
			log.error("解析XML错误", e);
			return null;
		}
	}
	/**
	 * 获取xml中的int值.
	 * 
	 * @param str
	 * @return
	 */
	public static Integer getInteger(String str) {
		return Integer.valueOf(getString(str));
	}

	/**
	 * 获取xml中的Double值.
	 * 
	 * @param str
	 * @return
	 */
	public static double getDouble(String str) {
		return Double.valueOf(getString(str));
	}


	/**
	 * 将XML中配置的一组数据解析为MAP
	 * 
	 * @param str
	 * @param splitStr
	 * @return
	 */
	public static Map<Integer, Double> getMapValue(String path, String splitStr) {
		Map<Integer, Double> map = new HashMap<Integer, Double>();
		String[] params = getString(path).split(splitStr);
		for (String value : params) {
			Integer key = Integer.valueOf(value.split("=")[0]);
			Double val = Double.valueOf(value.split("=")[1]);
			map.put(key, val);
		}
		return map;
	}
	/**
	 * 将XML中配置的一组数据解析为[]
	 * 
	 * @param str
	 * @param splitStr
	 * @return
	 */
	public static int[] getIntegerValue(String path) {
		String[] params = getString(path).split(",");
		return ArrayUtil.stringToIntArray(params);
	}
	/**
	 * 将XML中配置的一组数据解析为MAP
	 * 
	 * @param str
	 * @param splitStr
	 * @return
	 */
	public static Map<Integer, String> getStringMapValue(String path, String splitStr) {
		Map<Integer, String> map = new HashMap<Integer,String>();
		String[] params = getString(path).split(splitStr);
		for (String value : params) {
			Integer key = Integer.valueOf(value.split("=")[0]);
			String val = String.valueOf(value.split("=")[1]);
			map.put(key, val);
		}
		return map;
	}
	/**
	 * 将XML中配置的一组数据解析为MAP
	 * 
	 * @param str
	 * @param splitStr
	 * @return
	 */
	public static Map<Integer, Double> getMapValue(Element el, String splitStr) {
		Map<Integer, Double> map = new HashMap<Integer, Double>();
		String[] params = el.getTextTrim().split(splitStr);
		for (String value : params) {
			Integer key = Integer.valueOf(value.split("=")[0]);
			Double val = Double.valueOf(value.split("=")[1]);
			map.put(key, val);
		}
		return map;
	}

//	public static void main(String[] args) {
//		System.out.println(GameConfigUtil.getMapValue("strongEquip.scaleParam","#"));
//	
//	}
}
